<?php defined('SYSPATH') OR die('No direct access allowed.');

class Password_Plugin extends WB_FormItem {
	
	var $hidden = FALSE;
	var $prefix2, $suffix2, $description2, $label2;
	var $length = '6,12';
	var $use_num = FALSE;
	var $use_lower = FALSE;
	var $use_upper = FALSE;
	
	public function __construct($name='', $params=array())
	{
		parent::__construct($name, $params, 'text password');
		
		$this->hidden = in_array('hidden', $this->params) ? TRUE : $this->hidden;
		$this->prefix2 = isset($params['prefix2']) ? $params['prefix2'] : $this->prefix;
		$this->suffix2 = isset($params['suffix2']) ? $params['suffix2'] : $this->suffix;
		$this->description2 = isset($params['description2']) ? $params['description2'] : '';
		$this->attributes2 = isset($params['attributes2']) ? $params['attributes2'] : '';
		$this->label2 = 
				isset($params['label2']) ? 
					$params['label2'] :
					$this->label.' '.Kohana::lang('password_plugin.confirm_label');
		
		$this->length = isset($this->params['length']) ? $this->params['length'] : $this->length;
		$this->use_num = in_array('use_num', $this->params) ? TRUE : $this->use_num;
		$this->use_lower = in_array('use_lower', $this->params) ? TRUE : $this->use_lower;
		$this->use_upper = in_array('use_upper', $this->params) ? TRUE : $this->use_upper;
	}
	
	public function set_value($value, $type)
	{
		if ($type=='edit' || $type=='add')
		{
			//Kohana::log('info', 'Password_Plugin->set_value(): '.$value);
			if (empty($value)) $this->value = NULL;
			else
			{
				$confirm = Kohana::instance()->input->post($this->name.'_confirm');
				
				$length = explode(',', (string)$this->length);
				$min_length = (int)$length[0];
				$max_length = isset($length[1]) ? (int)$length[1] : 100;
				
				if ($confirm!=$value)
				{
					$this->error_msg = Kohana::lang('password_plugin.err_confirmation');
				}
				elseif ($min_length && strlen($value)<$min_length
						|| $max_length && strlen($value)>$max_length)
				{
					if ($min_length==$max_length)
						$this->error_msg = Kohana::lang('password_plugin.err_password_length_exact', $min_length);
					else 
						$this->error_msg = Kohana::lang('password_plugin.err_password_length_range', $min_length, $max_length);
				}
				elseif ($this->use_num && preg_match_all('|\d|',$value, $matches)<$this->use_num)
				{
					$this->error_msg = Kohana::lang('password_plugin.err_use_num', (int)$this->use_num);
				}
				elseif ($this->use_lower && preg_match_all('|[a-z]|',$value, $matches)<$this->use_lower)
				{
					$this->error_msg = Kohana::lang('password_plugin.err_use_lower', (int)$this->use_lower);
				}
				elseif ($this->use_upper && preg_match_all('|[A-Z]|',$value, $matches)<$this->use_upper)
				{
					$this->error_msg = Kohana::lang('password_plugin.err_use_upper', (int)$this->use_upper);
				}
				else $this->value = Kohana::instance()->auth->hash_password($value);
			}
			
		}
		else $this->value = $value;
	}

	
	/**
	 * Renders a table cell
	 * 
	 * @return string
	 */
	public function render_cell()
	{
		if ( ! $this->hidden) return parent::render_cell();
		else return Kohana::lang('password_plugin.hidden_cell');
	}

	
	/**
	 * Renders the form element
	 * 
	 * @return string
	 */
	public function render_item()
	{
		$confirm_name = $this->name.'_confirm';
		
		$str = $this->prefix;
		$str .= form::label($this->name, $this->label);
		$str .= $this->hidden ?
				form::password($this->name, '', html::attributes($this->attributes)) :
				form::input($this->name, '', html::attributes($this->attributes));
		if ($this->description) $str .= '<span class="description">'.$this->description.'</span>';
		$str .= $this->suffix;
		
		$str .= $this->prefix2;
		$str .= form::label($confirm_name, $this->label2);
		$str .= $this->hidden ?
				form::password($confirm_name, '', html::attributes($this->attributes2)) :
				form::input($confirm_name, '', html::attributes($this->attributes2));
		if ($this->description2) $str .= '<span class="description">'.$this->description2.'</span>';
		$str .= $this->suffix2;
		
		return $str;
	}
}

//End of Password_Plugin Library